﻿using System;
using System.Collections.Generic;
using System.Text;
using ESpace.DalTools.Contracts;
using System.IO;
using System.Data;
using System.Xml;

namespace ESpace.DalTools.SQLite
{
	public class BusinessContractsSQLite : BuilderBaseSQLite, IContractsBuilder 
	{
		#region 属性定义
		private string _DomainNameSpace;
		private string _SavePath;
		private string _TableName;
		private string _ContractsNameSpace;
		private XmlDocument xmlDoc;

		/// <summary>
		/// 业务层的保存路径
		/// </summary>
		public string SavePath
		{
			get
			{
				return _SavePath;
			}
			set
			{
				string sSplit = value.Substring(value.Length - 1);
				if (sSplit != @"\" && sSplit != "/")
					_SavePath = value + @"\";
				else
					_SavePath = value;
			}
		}

		/// <summary>
		/// 要生成服务层代码的表的名称
		/// </summary>
		public string TableName
		{
			get
			{
				return _TableName;
			}
			set
			{
				_TableName = value;
				InitMetaData();
			}
		}


		/// <summary>
		/// 服务层的名称空间
		/// </summary>
		public string ContractsNameSpace
		{
			get
			{
				return _ContractsNameSpace;
			}
			set
			{
				_ContractsNameSpace = value;
			}
		}


		/// <summary>
		/// 持久层的名称空间
		/// </summary>
		public string DomainNameSpace
		{
			get
			{
				return _DomainNameSpace;
			}
			set
			{
				_DomainNameSpace = value;
			}
		}

		#endregion

		private void InitMetaData()
		{
			DataTable dtColumns = GetColumns(_TableName);
			DataSet dsMetaData = new DataSet("Schema");
			dsMetaData.Tables.Add(dtColumns);

			int isHavePK = dtColumns.Select("IsPK=1").Length == 0 ? 0 : 1;
			int isAllPK = dtColumns.Select("IsPK=0").Length != 0 ? 0 : 1;

			xmlDoc = new XmlDocument();
			xmlDoc.LoadXml(dsMetaData.GetXml());

			XmlElement xmlElement = xmlDoc.DocumentElement;
			xmlElement.SetAttribute("TableName", _TableName);
			xmlElement.SetAttribute("IfHavePK", isHavePK.ToString());
			xmlElement.SetAttribute("IfAllPK", isAllPK.ToString());
			xmlElement.SetAttribute("DomainNameSpace", _DomainNameSpace);
			xmlElement.SetAttribute("ContractsNameSpace", _ContractsNameSpace);
			xmlElement.SetAttribute("UserName", "UserName");
			xmlElement.SetAttribute("UserEMail", "UserEMail");
			xmlElement.SetAttribute("ModifyDate", DateTime.Now.ToString());

			//xmlElement.InnerXml += GetForeignKeys(_TableName).InnerXml;
			//
			//#if DEBUG
			//			xmlDoc.Save(@"c:\SQLColumnsSchema.xml");
			//#endif
			//xmlDoc.Save(@"c:\test.xml");
		}

		/// <summary>
		/// 生成业务层代码
		/// </summary>
		public void BuilderContractsCode()
		{
			if (!Directory.Exists(_SavePath))
				Directory.CreateDirectory(_SavePath);

			string sFilePath = null;
			string sXsltPath = null;
			sFilePath = _SavePath + "I" + _TableName + "Service.cs";
			sXsltPath = GetCurrentExecutePath() + @"Template\Nhibernate\Contracts.xslt";
			if (File.Exists(sFilePath) && (File.GetAttributes(sFilePath) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
			{
				throw new System.IO.IOException(sFilePath + "只读");
			}

			TemplateTransform.TransformXslt(xmlDoc, sXsltPath, sFilePath);

		}
	}
}
